<?php
declare (strict_types=1);

namespace app\api\controller\v2;

use app\common\model\Banner;
use app\common\model\Cate;
use app\common\model\Picture;
use app\common\model\PictureCol;
use think\facade\Db;

class Sucai extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        return sucessMsg('200', '获取成功', [
            'banner' => $this->banner(),//获取素材banner图
            'cate' => $this->cate(),//获取素材分类
            'format' => $this->format(),//获取素材格式分类
        ]);
    }

    /**
     *
     *获取素材banner图
     */
    public function banner()
    {
        return Banner::where(['type' => 2, 'status' => 1])
            ->field('id,title,image')
            ->order('sort', 'asc')
            ->withAttr('image', function ($val, $data) {
                return env('app.yuming') . $val;
            })
            ->select();
    }

    /**
     *
     *获取素材分类
     */
    public function cate()
    {
        return Cate::where(['type' => 5, 'status' => 1])
            ->field('id,name')
            ->order('sort', 'asc')
            ->select();
    }

    /**
     *
     *获取素材格式分类
     */
    public function format()
    {
        return Picture::where('status', 1)
            ->field('id,format')
            ->order('sort', 'asc')
            ->group('format')
            ->select();
    }

    /**
     *
     *获取素材
     */
    public function sucai()
    {
        $page = input('page/d', 1);
        $cate_id = input('id/d');
        $format = input('format');
        $model = new Picture();
        $listRows = 16;
        $where['status'] = 1;
        if (!empty($cate_id)) $where['cate_id'] = $cate_id;
        if (!empty($format)) $where['format'] = $format;
        if (!empty($format) && !empty($cate_id)) {
            $where['cate_id'] = $cate_id;
            $where['format'] = $format;
        }
        $sucai = $model->where($where)
            ->field('id,image')
            ->order('sort', 'asc')
            ->page($page, $listRows)
            ->withAttr('image', function ($val, $data) {
                return env('app.yuming') . $val;
            })
            ->select();
        $count = $model->where($where)->count();
        $maxpage = ceil($count / $listRows);
        return sucessMsg('200', '获取成功', ['sucai' => $sucai, 'page' => $page, 'maxpage' => $maxpage]);
    }

    public function details()
    {
        $id = input('id/d');
        if (empty($id)) {
            return '无id查询';
        }
        $details = Picture::where(['status' => 1, 'id' => $id])
            ->find();
        if (empty($details)) {
            return '页面丢失，请稍后再试！';
        }
        $details['created_at'] = date('Y/m/d H:i:s');
        $details['image'] = env('app.yuming') . $details['image'];
        $details['param'] = json_decode($details['param']);
        $xiangguan = Picture::where('status', 1)
            ->field('id,image,title')
            ->withAttr('image', function ($val, $data) {
                return env('app.yuming') . $val;
            })
            ->orderRaw("rand() , id desc")
            ->limit(0, 4)
            ->select();
        //判断是否收藏
        $is_col = PictureCol::where(['status' => 1, 'picture_id' => $id, 'col_member_id' => $this->user_id])->count();
        $details['is_col'] = 0;
        if ($is_col > 0) $details['is_col'] = 1;
        return sucessMsg('200', '获取成功', ['details' => $details, 'xiangguan' => $xiangguan]);
    }

    public function col()
    {
        $picture_id = input('id/d', 1);
        $is_col = input('is_col/d', 0);
        $model = New Picture();
        $picture_colModel = New PictureCol();
        $data = [];
        $data['col_member_id'] = $this->user_id;
        $data['member_id'] = $model->where(['status' => 1, 'id' => $picture_id])
            ->value('member_id');
        $data['picture_id'] = $picture_id;
        if ($is_col == 0) {
            $data['is_col'] = 1;
            $aa = $picture_colModel->save($data);
            if (!$aa) {
                return errMsg('400', '系统繁忙，请稍后再试');
            }
            return sucessMsg('200', '收藏成功');
        } else {
            $bb = $picture_colModel->where(['col_member_id' => $data['col_member_id'],
                'member_id' => $data['member_id'],
                'picture_id' => $data['picture_id']
            ])->delete();
            if (!$bb) {
                return errMsg('400', '系统繁忙，请稍后再试');
            }
            return sucessMsg('200', '取消收藏成功');
        }
    }
}
